package com.bawei.es.controller;

import com.alibaba.fastjson.JSONObject;
import com.bawei.common.core.domain.R;
import com.bawei.domain.request.IndexInfoReq;
import com.bawei.domain.response.IndexInfoResp;
import com.bawei.es.service.EsIndexService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/es/index")
@Slf4j
public class EsIndexController {
    @Autowired
    private EsIndexService esIndexService;

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/createIndex")
    public R createIndex(@RequestBody IndexInfoReq indexInfoReq){
        log.info("功能名称:Es索引的创建,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(indexInfoReq));
        R index = esIndexService.createIndex(indexInfoReq);
        log.info("功能名称:Es索引的创建,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(index));
        return R.ok(index);
    }


    @GetMapping("/deleteIndex")
    public  R deleteIndex(@RequestParam String indexName){
        log.info("功能名称:Es索引的删除,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                indexName);
        R r = esIndexService.deleteIndex(indexName);
        log.info("功能名称:Es索引的删除,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(r));
        return R.ok(r);
    }

    @GetMapping("/isExits")
    public  Boolean isExits(@RequestParam String indexName){
        log.info("功能名称:判断Es索引是否存在,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                indexName);
        Boolean exitIndex = esIndexService.isExitIndex(indexName);
        log.info("功能名称:判断Es索引是否存在,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(exitIndex));
        return exitIndex;
    }


    @PostMapping("/modifyIndex")
    public  void modifyIndex(@RequestParam String newIndexName,@RequestBody IndexInfoReq indexInfoReq){
        log.info("功能名称:Es索引修改,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                newIndexName,indexInfoReq);
        esIndexService.modifyIndex(newIndexName, indexInfoReq);
    }


    @PostMapping("/getIndex")
    public  R<IndexInfoResp> getIndex(@RequestParam String indexName){
        log.info("功能名称:根据索引查询,请求URI:{},请求方式:{},请求参数:{}",
                request.getRequestURI(),
                request.getMethod(),
                indexName);
        IndexInfoResp index = esIndexService.getIndex(indexName);
        log.info("功能名称:根据索引查询,请求URI:{},请求方式:{},响应结果:{}",
                request.getRequestURI(),
                request.getMethod(),
                JSONObject.toJSONString(index));
        return R.ok(index);
    }



}
